package org.pz.controller;

import org.pz.feign.FeignTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@CrossOrigin
@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignTest feignTest;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/test")
    public void test(){
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos-server");
        System.out.println(instances);
    }

    @RequestMapping("/restTemplateTest")
    public String restTemplateTest() {
        String res = restTemplate.getForObject("http://nacos-server/test/", String.class);
        System.out.println("返回结果：" + res);
        return res;
    }

    @RequestMapping("/feignTest")
    public String feignTest() {
        String test = feignTest.test();
        System.out.println("使用 feign 调用返回结果：" + test);
        return test;
    }
}
